import React from 'react'
import { connect } from 'dva'
import { withRouter } from 'react-router-dom'

import Bar from '../Bar'

import './style.css'


const CatrBar = ({ cart, dispatch, history }) => {
    
    const cleanChecked = () => {
        dispatch({
            type: 'cart/cleanChecked'
        })
    }

    let left = [
        <span 
            key={1} 
            className={`cartBar-chooseAll_checked ${cart.total.chooseAll && 'cartBar-chooseAll_checked_active'}`}
            onClick={cleanChecked}
        >
            <i className='iconfont icon-danyehuaban' ></i>
        </span>,
        <span key={2} style={{ fontSize: 14 }}>
            全选
        </span>
    ]
    
    let right = (
        <div className='cartBar-buy_money'>
            <span>合计:</span>
            <span>¥ {cart.total.money}</span>
        </div>
    )
    const buy = () => {
        let isChooseAll = false
        for (let item of cart.cart.list) {
            if (item.checked) {
                isChooseAll = true
                break;
            }
        }
        if (cart.cart.list.length > 0 && isChooseAll) {
            dispatch({ type: 'order/createrOrder' })
            history.push('/createOrder')
        }
    }
    return (
        <Bar 
            left={left}
            right={right}
            clickBuy={buy}
        />
    )
}

const mapState = state => ({
    cart: state.cart
})

export default connect(mapState)(withRouter(CatrBar))
